package com.cubesugar2014.withchild;

import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImageAndTextAdapter extends ArrayAdapter<String> {

    private LayoutInflater mInflater;
    
    private String[] mStrings;
    private TypedArray mIcons;
    
    private String[] mPackageName;
    
    private Context mContext;
    
    private int mViewResourceId;
    
    public ImageAndTextAdapter(Context ctx, int viewResourceId,
            String[] strings, TypedArray icons) {
        super(ctx, viewResourceId, strings);
        
        mInflater = (LayoutInflater)ctx.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        
        mStrings = strings;
        mIcons = icons;
        
        mViewResourceId = viewResourceId;
    }
    
    public ImageAndTextAdapter(Context ctx, int viewResourceId,
            String[] strings, String[] packName) {
        super(ctx, viewResourceId, strings);
        
        mInflater = (LayoutInflater)ctx.getSystemService(
                Context.LAYOUT_INFLATER_SERVICE);
        
        mStrings = strings;
        mIcons = null;
        mPackageName = packName;
        
        mViewResourceId = viewResourceId;
        
        mContext = ctx;
    }
    

    @Override
    public int getCount() {
        return mStrings.length;
    }

    @Override
    public String getItem(int position) {
        return mStrings[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private View _getDropdownView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);

        if (mIcons != null)
        {
        	ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
        	iv.setImageDrawable(mIcons.getDrawable(position));
        }
        else
        {
           	ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
           
        	Drawable icon = null;
        	
        	try {
        		icon =  mContext.getPackageManager().getApplicationIcon(mPackageName[position]);
        		
        		iv.setImageDrawable(icon);
                
        	} catch (NameNotFoundException e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();

        	}

        	
        }
        
        TextView tv = (TextView)convertView.findViewById(R.id.option_text);
        tv.setText(mStrings[position]);
        
        return convertView;
    }
    
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        convertView = mInflater.inflate(mViewResourceId, null);

       //only text
        ImageView iv = (ImageView)convertView.findViewById(R.id.option_icon);
        iv.setImageDrawable(null);
               
        TextView tv = (TextView)convertView.findViewById(R.id.option_text);
        tv.setText(mStrings[position]);
        
        return convertView;
    }
    
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        return _getDropdownView(position, convertView, parent);
    }

    
}